// component/add_goods_info/add_goods_info.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  
  data: {
    index: 1,
    indexs: 0,
    indext: 0,
    array: ['泡货', '重货'],
    packaging: ['托盘', '纸箱'],
    types: ['快消品', '大件货物'],
    show_alert: false,
    but_add: false,
    but_bao: true,
    goods_info:{
      goods_name:'',
      goods_type:'',
      goods_weight: '',
      goods_pack: '',
      goods_lei:'',
    },
    goods_length:0
  },
  
  methods:{
    switch1Change: function (e) {
      console.log('switch1 发生 change 事件，携带值为', e.detail.value)
    },
    change_goods_name(e){
      let goods_info = this.data.goods_info;
      goods_info.goods_name = e.detail.value;
      this.setData({
        goods_info
      })     
    },
    bindPickerChange(e) {
      let goods_info = this.data.goods_info;
      goods_info.goods_type = this.data.array[e.detail.value];
      this.setData({
        goods_info,
        index: e.detail.value
      })
    },
    //货物类型 
    typsPickerChange: function (e) {
      let goods_info = this.data.goods_info;
      goods_info.goods_pack = this.data.types[e.detail.value];
      this.setData({
        goods_info,
        indext: e.detail.value
      })
    },
    change_goods_weight(e) {
      let goods_info = this.data.goods_info;
      goods_info.goods_weight = e.detail.value;
      this.setData({
        goods_info
      })
    }, 
    //货物包装 
    baoPickerChange: function (e) {
      let goods_info = this.data.goods_info;
      goods_info.goods_lei = this.data.packaging[e.detail.value];
      this.setData({
        goods_info,
        indexs: e.detail.value
      })
    },
    save_goods() {
      // 货物表单组件保存数据时，先查询本地缓存中的 货物信息
      let goods_list = wx.getStorageSync("publish_goods_list");
      console.log(goods_list)
      goods_list.push(this.data.goods_info);
      wx.setStorageSync("publish_goods_list", goods_list);
      console.log("save succ")
      this.triggerEvent('savesucc', {}, {})
    },
    onMyEvent: function (e) {
      this.setData({
        show_alert: false
      })
    },
    start_save() {
      this.setData({
        show_alert: true
      })
    },
  },
  ready(){
    console.log("created");
    // 每次打开组件 都会触发的 方法
    // 在这个方法中 获取当前的货物信息
    let goods_list = wx.getStorageSync("publish_goods_list");
    console.log(goods_list)
    this.setData({
      goods_length: goods_list.length+1
    })
  }
 
})
